﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmFamilyInfo : Form
    {
        FamilyInfo _FamilyInfo;
        int _EmployeeID;
        bool isChange = false;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        FamilyClient wcfFamily;
        public frmFamilyInfo(int FamilyID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //LoadLanguage();
            dlgResult = DialogResult.Cancel;
            _FamilyInfo = new FamilyInfo();
            wcfFamily = Service.Family();
            _FamilyInfo.ID = FamilyID;
            _EmployeeID = EmployeeID;

            if (FamilyID > 0)
            {
                _FamilyInfo = wcfFamily.Family_GetInfo(FamilyID);
                Info2Form();
            }
            listRole = _listRole;
        }

        public void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }

        void Info2Form()
        {
            txtFamily_Name.Text = _FamilyInfo.Name;
            txtFamily_RelationShip.Text = _FamilyInfo.Relationship;
            txtFamily_Occupation.Text = _FamilyInfo.Occupation;
            txtDOB.Text = _FamilyInfo.BirthYear;
            txtFamily_Address.Text = _FamilyInfo.Address;
            txtFamily_Description.Text = _FamilyInfo.Description;
            chkIsdependent.Checked = _FamilyInfo.IsDependent ? true : false;
            isChange = false;
        }

        void Form2Info()
        {
            _FamilyInfo.EmployeeID = _EmployeeID;
            _FamilyInfo.Name = txtFamily_Name.Text;
            _FamilyInfo.Relationship = txtFamily_RelationShip.Text;
            _FamilyInfo.BirthYear = txtDOB.Text;
            _FamilyInfo.Occupation = txtFamily_Occupation.Text;
            _FamilyInfo.Address = txtFamily_Address.Text;
            _FamilyInfo.Description = txtFamily_Description.Text;
            _FamilyInfo.IsDependent = chkIsdependent.Checked ? true : false;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (CheckTextValid.IsEmpty(txtFamily_Name.Text))
            {
                txtFamily_Name.Focus();
                MessageBox.Show(UpdateUI.GetString("tennguoithankhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            //if (!CheckTextValid.IsInteger(txtDOB.Text))
            //{
            //    txtDOB.Focus();
            //    MessageBox.Show(UpdateUI.GetString("namsinhcuanguoithankhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_FamilyInfo.ID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfFamily.Family_Update(_FamilyInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _FamilyInfo.ID = wcfFamily.Family_Insert(_FamilyInfo);
                if (_FamilyInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }


        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_FamilyInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _FamilyInfo = new FamilyInfo();
            _FamilyInfo.ID = 0;
            txtFamily_Name.Text = "";
            txtFamily_Name.Focus();
            txtFamily_RelationShip.Text = "";
            txtFamily_Occupation.Text = "";
            txtDOB.Text = "";
            txtFamily_Address.Text = "";
            txtFamily_Description.Text = "";
        }

        private void frmFamilyInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            // wcfFamily.Close();
            this.DialogResult = dlgResult;
        }

        private void frmFamilyInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
